/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2s.gameserver.scripts.quests;

import l2s.gameserver.model.actor.L2Npc;
import l2s.gameserver.model.actor.instance.L2PcInstance;
import l2s.gameserver.model.quest.Quest;
import l2s.gameserver.model.quest.QuestState;
import l2s.gameserver.scripting.ScriptFile;

/**
 * Квест на вторую профессию Trial Of Seeker
 *
 * @author Sergey Ibryaev aka Artful
 */
public class Q213_TrialOfTheSeeker extends Quest implements ScriptFile
{
	private static final String qn = "Q213_TrialOfTheSeeker";
	private static final int Dufner = 30106;
	private static final int Terry = 30064;
	private static final int Viktor = 30684;
	private static final int Marina = 30715;
	private static final int Brunon = 30526;
	private static final int DufnersLetter = 2647;
	private static final int Terrys1stOrder = 2648;
	private static final int Terrys2ndOrder = 2649;
	private static final int TerrysLetter = 2650;
	private static final int ViktorsLetter = 2651;
	private static final int HawkeyesLetter = 2652;
	private static final int MysteriousRunestone = 2653;
	private static final int OlMahumRunestone = 2654;
	private static final int TurekRunestone = 2655;
	private static final int AntRunestone = 2656;
	private static final int TurakBugbearRunestone = 2657;
	private static final int TerrysBox = 2658;
	private static final int ViktorsRequest = 2659;
	private static final int MedusaScales = 2660;
	private static final int ShilensRunestone = 2661;
	private static final int AnalysisRequest = 2662;
	private static final int MarinasLetter = 2663;
	private static final int ExperimentTools = 2664;
	private static final int AnalysisResult = 2665;
	private static final int Terrys3rdOrder = 2666;
	private static final int ListOfHost = 2667;
	private static final int AbyssRunestone1 = 2668;
	private static final int AbyssRunestone2 = 2669;
	private static final int AbyssRunestone3 = 2670;
	private static final int AbyssRunestone4 = 2671;
	private static final int TerrysReport = 2672;
	private static final int MarkofSeeker = 2673;
	private static final int NeerGhoulBerserker = 20198;
	private static final int OlMahumCaptain = 20211;
	private static final int TurekOrcWarlord = 20495;
	private static final int AntCaptain = 20080;
	private static final int TurakBugbearWarrior = 20249;
	private static final int Medusa = 20158;
	private static final int MarshStakatoDrone = 20234;
	private static final int BrekaOrcOverlord = 20270;
	private static final int AntWarriorCaptain = 20088;
	private static final int LetoLizardmanWarrior = 20580;
	private static final int[][] DROPLIST_COND =
	{
		{
			2,
			3,
			NeerGhoulBerserker,
			Terrys1stOrder,
			MysteriousRunestone,
			1,
			10,
			1
		},
		{
			4,
			0,
			OlMahumCaptain,
			Terrys2ndOrder,
			OlMahumRunestone,
			1,
			20,
			1
		},
		{
			4,
			0,
			TurekOrcWarlord,
			Terrys2ndOrder,
			TurekRunestone,
			1,
			20,
			1
		},
		{
			4,
			0,
			AntCaptain,
			Terrys2ndOrder,
			AntRunestone,
			1,
			20,
			1
		},
		{
			4,
			0,
			TurakBugbearWarrior,
			Terrys2ndOrder,
			TurakBugbearRunestone,
			1,
			20,
			1
		},
		{
			9,
			10,
			Medusa,
			ViktorsRequest,
			MedusaScales,
			10,
			30,
			1
		},
		{
			16,
			0,
			MarshStakatoDrone,
			ListOfHost,
			AbyssRunestone1,
			1,
			25,
			1
		},
		{
			16,
			0,
			BrekaOrcOverlord,
			ListOfHost,
			AbyssRunestone2,
			1,
			25,
			1
		},
		{
			16,
			0,
			AntWarriorCaptain,
			ListOfHost,
			AbyssRunestone3,
			1,
			25,
			1
		},
		{
			16,
			0,
			LetoLizardmanWarrior,
			ListOfHost,
			AbyssRunestone4,
			1,
			25,
			1
		}
	};
	private static boolean QuestProf = true;
	
	public Q213_TrialOfTheSeeker(int questId, String name, String descr)
	{
		super(questId, name, descr);
		addStartNpc(Dufner);
		addTalkId(Dufner);
		addTalkId(Terry);
		addTalkId(Viktor);
		addTalkId(Marina);
		addTalkId(Brunon);
		for (int[] element : DROPLIST_COND)
		{
			addKillId(element[2]);
		}
		questItemIds = new int[]
		{
			DufnersLetter,
			Terrys1stOrder,
			Terrys2ndOrder,
			TerrysLetter,
			TerrysBox,
			ViktorsLetter,
			ViktorsRequest,
			HawkeyesLetter,
			ShilensRunestone,
			AnalysisRequest,
			MarinasLetter,
			ExperimentTools,
			AnalysisResult,
			ListOfHost,
			Terrys3rdOrder,
			TerrysReport,
			MysteriousRunestone,
			OlMahumRunestone,
			TurekRunestone,
			AntRunestone,
			TurakBugbearRunestone,
			MedusaScales,
			AbyssRunestone1,
			AbyssRunestone2,
			AbyssRunestone3,
			AbyssRunestone4
		};
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (event.equalsIgnoreCase("30106-05.htm"))
		{
			st.set("cond", "1");
			st.setState(Quest.STATE_STARTED);
			st.playSound(QuestState.SOUND_ACCEPT);
			st.giveItems(DufnersLetter, 1);
			st.giveItems(7562, 64);
		}
		else if (event.equalsIgnoreCase("30064-03.htm"))
		{
			st.giveItems(Terrys1stOrder, 1);
			st.takeItems(DufnersLetter, -1);
			st.set("cond", "2");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30064-07.htm"))
		{
			st.takeItems(Terrys1stOrder, -1);
			st.takeItems(MysteriousRunestone, -1);
			st.giveItems(Terrys2ndOrder, 1);
			st.set("cond", "4");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30064-10.htm"))
		{
			st.takeItems(Terrys2ndOrder, -1);
			st.takeItems(OlMahumRunestone, -1);
			st.takeItems(TurekRunestone, -1);
			st.takeItems(AntRunestone, -1);
			st.takeItems(TurakBugbearRunestone, -1);
			st.giveItems(TerrysLetter, 1);
			st.giveItems(TerrysBox, 1);
			st.set("cond", "6");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30684-05.htm"))
		{
			st.takeItems(TerrysLetter, -1);
			st.giveItems(ViktorsLetter, 1);
			st.set("cond", "7");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30684-11.htm"))
		{
			st.takeItems(TerrysLetter, -1);
			st.takeItems(TerrysBox, -1);
			st.takeItems(HawkeyesLetter, -1);
			st.giveItems(ViktorsRequest, 1);
			st.set("cond", "9");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30684-15.htm"))
		{
			st.takeItems(ViktorsRequest, -1);
			st.takeItems(MedusaScales, -1);
			st.giveItems(ShilensRunestone, 1);
			st.giveItems(AnalysisRequest, 1);
			st.set("cond", "11");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30715-02.htm"))
		{
			st.takeItems(ShilensRunestone, -1);
			st.takeItems(AnalysisRequest, -1);
			st.giveItems(MarinasLetter, 1);
			st.set("cond", "12");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30715-05.htm"))
		{
			st.takeItems(ExperimentTools, 1);
			st.giveItems(AnalysisResult, 1);
			st.set("cond", "14");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30064-18.htm"))
		{
			if (st.getPlayer().getLevel() < 36)
			{
				htmltext = "30064-17.htm";
				st.takeItems(AnalysisResult, -1);
				st.giveItems(Terrys3rdOrder, 1);
			}
			else
			{
				htmltext = "30064-18.htm";
				st.giveItems(ListOfHost, 1);
				st.takeItems(AnalysisResult, -1);
				st.set("cond", "16");
				st.setState(Quest.STATE_STARTED);
			}
		}
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		int npcId = npc.getNpcId();
		String htmltext = getNoQuestMsg();
		QuestState st = player.getQuestState(qn);
		int cond = st.getInt("cond");
		if (npcId == Dufner)
		{
			if (st.getQuestItemsCount(MarkofSeeker) != 0)
			{
				htmltext = getAlreadyCompletedMsg();
				st.exitQuest(true);
			}
			else if ((cond == 0) && (st.getQuestItemsCount(TerrysReport) == 0))
			{
				if ((st.getPlayer().getClassId().ordinal() == 0x07) || (st.getPlayer().getClassId().ordinal() == 0x16) || (st.getPlayer().getClassId().ordinal() == 0x23))
				{
					if (st.getPlayer().getLevel() < 35)
					{
						htmltext = "30630-02.htm";
						st.exitQuest(true);
					}
					else
					{
						htmltext = "30106-03.htm";
					}
				}
				else
				{
					htmltext = "30106-01.htm";
					st.exitQuest(true);
				}
			}
			else if ((st.getQuestItemsCount(DufnersLetter) == 1) && (st.getQuestItemsCount(TerrysReport) == 0))
			{
				htmltext = "30106-06.htm";
			}
			else if ((st.getQuestItemsCount(DufnersLetter) == 0) && (st.getQuestItemsCount(TerrysReport) == 0))
			{
				htmltext = "30106-07.htm";
			}
			else if (st.getQuestItemsCount(TerrysReport) != 0)
			{
				st.getPlayer().addExpAndSp(27000, 3100);
				htmltext = "30106-08.htm";
				st.playSound(QuestState.SOUND_FINISH);
				st.takeItems(TerrysReport, -1);
				st.giveItems(MarkofSeeker, 1);
				st.exitQuest(true);
			}
		}
		else if (npcId == Terry)
		{
			if (cond == 1)
			{
				htmltext = "30064-01.htm";
			}
			else if (cond == 2)
			{
				htmltext = "30064-04.htm";
			}
			else if ((cond == 3) && (st.getQuestItemsCount(MysteriousRunestone) == 0))
			{
				htmltext = "30064-04.htm";
				st.set("cond", "2");
			}
			else if (cond == 3)
			{
				htmltext = "30064-05.htm";
			}
			else if (cond == 4)
			{
				htmltext = "30064-07.htm";
			}
			else if ((cond == 5) && (st.getQuestItemsCount(OlMahumRunestone) != 0) && (st.getQuestItemsCount(TurekRunestone) != 0) && (st.getQuestItemsCount(AntRunestone) != 0) && (st.getQuestItemsCount(TurakBugbearRunestone) != 0))
			{
				htmltext = "30064-09.htm";
			}
			else if (cond == 5)
			{
				htmltext = "30064-07.htm";
				st.set("cond", "4");
			}
			else if (cond == 6)
			{
				htmltext = "30064-11.htm";
			}
			else if (cond == 7)
			{
				st.takeItems(ViktorsLetter, -1);
				st.giveItems(HawkeyesLetter, 1);
				htmltext = "30064-12.htm";
				st.set("cond", "8");
				st.setState(Quest.STATE_STARTED);
			}
			else if (cond == 8)
			{
				htmltext = "30064-13.htm";
			}
			else if ((cond > 8) && (cond < 14))
			{
				htmltext = "30064-14.htm";
			}
			else if ((cond == 14) && (st.getQuestItemsCount(AnalysisResult) > 0))
			{
				htmltext = "30064-15.htm";
			}
			else if (((cond == 14) || (cond == 15)) && (st.getQuestItemsCount(Terrys3rdOrder) > 0))
			{
				if (st.getPlayer().getLevel() < 36)
				{
					htmltext = "30064-20.htm";
				}
				else
				{
					htmltext = "30064-21.htm";
					st.takeItems(Terrys3rdOrder, -1);
					st.giveItems(ListOfHost, 1);
					st.set("cond", "16");
					st.setState(Quest.STATE_STARTED);
				}
			}
			else if ((cond == 15) || (cond == 16)) // 15 конда пока нету, и хз как
			{
				htmltext = "30064-22.htm";
			}
			else if (cond == 17)
			{
				if ((st.getQuestItemsCount(AbyssRunestone1) != 0) && (st.getQuestItemsCount(AbyssRunestone2) != 0) && (st.getQuestItemsCount(AbyssRunestone3) != 0) && (st.getQuestItemsCount(AbyssRunestone4) != 0))
				{
					htmltext = "30064-23.htm";
					st.takeItems(ListOfHost, -1);
					st.takeItems(AbyssRunestone1, -1);
					st.takeItems(AbyssRunestone2, -1);
					st.takeItems(AbyssRunestone3, -1);
					st.takeItems(AbyssRunestone4, -1);
					st.giveItems(TerrysReport, 1);
					st.set("cond", "0"); // Тут непонятки, в клиенте нету когда
				}
				else
				{
					htmltext = "30064-22.htm";
					st.set("cond", "16");
				}
			}
		}
		else if (npcId == Viktor)
		{
			if (cond == 6)
			{
				htmltext = "30684-01.htm";
			}
			else if (cond == 8)
			{
				htmltext = "30684-12.htm";
			}
			else if (cond == 9)
			{
				htmltext = "30684-13.htm";
			}
			else if ((cond == 10) && (st.getQuestItemsCount(MedusaScales) >= 10))
			{
				htmltext = "30684-14.htm";
			}
			else if (cond == 10)
			{
				st.set("cond", "9");
				htmltext = "30684-13.htm";
			}
		}
		else if (npcId == Marina)
		{
			if (cond == 11)
			{
				htmltext = "30715-01.htm";
			}
			else if (cond == 12)
			{
				htmltext = "30715-03.htm";
			}
			else if (cond == 13)
			{
				htmltext = "30715-04.htm";
			}
			else if (cond > 13)
			{
				htmltext = "30715-06.htm";
			}
		}
		else if (npcId == Brunon)
		{
			if (cond == 12)
			{
				htmltext = "30526-01.htm";
				st.takeItems(MarinasLetter, 1);
				st.giveItems(ExperimentTools, 1);
				st.set("cond", "13");
				st.setState(Quest.STATE_STARTED);
			}
			else if (cond == 13)
			{
				htmltext = "30526-02.htm";
			}
		}
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = checkPlayerState(player, npc, STATE_STARTED);
		if (st == null)
		{
			return null;
		}
		int npcId = npc.getNpcId();
		int cond = st.getInt("cond");
		for (int[] element : DROPLIST_COND)
		{
			if ((cond == element[0]) && (npcId == element[2]))
			{
				if ((element[3] == 0) || (st.getQuestItemsCount(element[3]) > 0))
				{
					if (element[5] == 0)
					{
						st.rollAndGive(element[4], element[7], element[6], QuestProf);
					}
					else if (st.rollAndGive(element[4], element[7], element[7], element[5], element[6], QuestProf))
					{
						if ((element[1] != cond) && (element[1] != 0))
						{
							st.set("cond", String.valueOf(element[1]));
							st.setState(Quest.STATE_STARTED);
						}
					}
				}
			}
		}
		if ((cond == 4) && (st.getQuestItemsCount(OlMahumRunestone) != 0) && (st.getQuestItemsCount(TurekRunestone) != 0) && (st.getQuestItemsCount(AntRunestone) != 0) && (st.getQuestItemsCount(TurakBugbearRunestone) != 0))
		{
			st.set("cond", "5");
			st.setState(Quest.STATE_STARTED);
		}
		else if ((cond == 16) && (st.getQuestItemsCount(AbyssRunestone1) != 0) && (st.getQuestItemsCount(AbyssRunestone2) != 0) && (st.getQuestItemsCount(AbyssRunestone3) != 0) && (st.getQuestItemsCount(AbyssRunestone4) != 0))
		{
			st.set("cond", "17");
			st.setState(Quest.STATE_STARTED);
		}
		return null;
	}
	
	public static void onLoad()
	{
		new Q213_TrialOfTheSeeker(213, qn, "Trial Of The Seeker");
	}
}
